package br.unicamp.ic.lis.dbvapi.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "Scale", uniqueConstraints = @UniqueConstraint(columnNames = {
		"type", "value" }))
public class Scale implements Serializable, Comparable<Scale> {

	private static final long serialVersionUID = 9185584245119062979L;

	public static enum ScaleType {
		SPATIAL_SCALE, TEMPORAL_SCALE
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "did")
	private long id;

	@OneToOne(cascade = CascadeType.ALL)
	private Scale parent;

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	private ScaleType type;

	@Column(name = "value")
	private String value;

	public Scale() {

	}

	public Scale(Scale parent, ScaleType type, String value) {
		this.parent = parent;
		this.type = type;
		this.value = value;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Scale getParent() {
		return parent;
	}

	public void setParent(Scale parent) {
		this.parent = parent;
	}

	public ScaleType getType() {
		return type;
	}

	public void setType(ScaleType type) {
		this.type = type;
	}

	@Override
	public String toString() {
		Long parentId = null;
		if (this.getParent() != null) {
			parentId = this.getParent().getId();
		}

		return "Scale [id=" + id + ", parent=" + parentId + ", type=" + type
				+ ", value=" + value + "]";
	}

	@Override
	public int compareTo(Scale other) {
		final int BEFORE = -1;
		// final int EQUAL = 0;
		final int AFTER = 1;

		if (this.getId() <= other.getId()) {
			return BEFORE;
		}
		return AFTER;
	}
}
